<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    li {
      list-style: none;
    }

    html,
    body,
    #box {
      width: 100%;
      height: 100%;
    }

    #box {
      background-color: #eee;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }

    .footer {
      width: 100%;
      height: 40px;
      background-color: skyblue;
      display: flex;
      justify-content: space-around;
      line-height: 40px;
    }

    .content {
      width: 100%;
      flex: 1;
      background-color: orange;
    }
  </style>
</head>

<body>
  <div id="box">
    <div class="content">
      <!-- 这是页面内容的容器 -->

    </div>
    <ul class="footer">
      <li><a href="#/home">首页</a></li>
      <li><a href="#/goodstype">商品分类</a></li>
      <li><a href="#/car">购物车</a></li>
      <li><a href="#/collection">收藏</a></li>
      <li><a href="#/my">我的</a></li>
    </ul>
  </div>

  <script src="./js/home.js"></script>
  <script src="./js/goodstype.js"></script>
  <script src="./js/car.js"></script>
  <script src="./js/my.js"></script>
  <script src="./js/collection.js"></script>

  <script>

    // 路由配置

    const routes = [
      {
        path:"/home",
        component:home
      },
      {
        path:"/goodstype",
        component:goodstype
      },
      {
        path:"/car",
        component:car
      },
      {
        path:"/my",
        component:my
      },
      {
        path:"/collection",
        component:collection
      },
      // {
      //   path:"*",
      //   component NotFound
      // }
    ]

    window.onload = function(){
      let hash = location.hash;
      if(!hash){
        // hash = "#/home";
        location.hash = "#/home";
      }
      goPage();
    }

    window.onhashchange = function () {
        goPage();
    }

    function goPage(){
      let hash = location.hash;// #/home
      let path = hash.slice(1);

      let route = routes.filter(item=>item.path==path)
      if(route.length>0){
        document.querySelector(".content").innerHTML =  route[0].component;
      }else{
        console.log("没有匹配到路由");
      }

    }

  </script>
</body>

</html>